package Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class filter_meal implements Filter {

	public void init(FilterConfig arg0) throws ServletException {

	}
	
	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();

		String requestURI = request.getRequestURI();
		if (requestURI.endsWith("login.jsp")
				|| requestURI.endsWith("bootstrap.min.css")
				|| requestURI.endsWith("logo.png")
				|| requestURI.endsWith("sigin.css")
				|| requestURI.endsWith("jquery.min.js")) {
			chain.doFilter(request, response);
			return;
		}
		if (session.getAttribute("username") != null) {
			chain.doFilter(req, res);
		} else {
			request.getRequestDispatcher("/Login_Servlet").forward(request,
					response);
		}
	}
}